<!-- 新增编辑车辆 -->
<template>
  <!-- 新增编辑车辆 -->
  <el-dialog
    title="新增车辆"
    :visible="showAddDialog"
    width="600px"
    @close="close"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item
        label="车辆类型"
        prop="truckTypeId"
      >
        <el-select
          v-model="form.truckTypeId"
          clearable
          placeholder="请选择"
        >
          <el-option
            v-for="item in CarTypeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        prop="licensePlate"
        label="车牌号码"
      >
        <el-input
          v-model="form.licensePlate"
          placeholder="请输入车牌号码"
        ></el-input>
      </el-form-item>
      <el-form-item
        prop="deviceGpsId"
        label="GPS设备ID"
      >
        <el-input
          v-model="form.deviceGpsId"
          placeholder="请输入GPS设备ID"
        ></el-input>
      </el-form-item>
    </el-form>
    <div
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        type="danger"
        @click="btnOk"
      >确认</el-button>
      <el-button @click="close">取消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getCarTypeList, truckTypeAdd } from '@/api/transit'
export default {
  name: 'VehicleAdd',
  props: {
    showAddDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        truckTypeId: null, // 车辆类型Id
        licensePlate: '', // 车牌号码
        deviceGpsId: '' // GPS设备ID

      },
      rules: {
        truckTypeId: [
          { required: true, message: '车辆类型不能为空', trigger: 'blur' }
        ],
        licensePlate: [
          { required: true, message: '车牌号码不能为空', trigger: 'blur' },
          {
            pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/,
            message: '请输入正确格式的车牌号',
            trigger: 'blur'
          }
        ],
        deviceGpsId: [
          { required: true, message: 'GPS设备ID不能为空', trigger: 'blur' }
        ]
      },
      CarTypeList: []
    }
  },
  created() {
    this.getCarTypeList()
  },
  methods: {
    // 获取车辆类型下拉
    async getCarTypeList() {
      const { data } = await getCarTypeList()
      // console.log(data)
      this.CarTypeList = data
    },
    // 关闭弹框
    close() {
      this.$emit('update:showAddDialog', false)
      this.$refs.form.resetFields()
    },
    // 添加车辆
    async btnOk() {
      await this.$refs.form.validate()
      await truckTypeAdd(this.form)
      this.$emit('update')
      this.$message.success('新增车辆成功')
      this.close()
    }
  }
}
</script>
<style scoped lang="scss">
/deep/ .el-dialog__body {
  padding: 40px 50px 0 20px !important;
}
::v-deep .el-form-item__content {
  margin-bottom: 25px;
}
::v-deep .el-form-item__label {
  font-weight: 400;
}
.dialog-footer {
  text-align: center;
}
.el-button {
  padding: 12px 30px;
}
</style>
